package com.xilihui.xlh.business.adapters;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.ApplyActivity;
import com.xilihui.xlh.business.activitys.AppointmentActivity;
import com.xilihui.xlh.business.activitys.EvaluationStoreActivity;
import com.xilihui.xlh.business.activitys.JoinStoresActivity;
import com.xilihui.xlh.business.activitys.GoAppointmenActivity;
import com.xilihui.xlh.business.activitys.LoginActivity;
import com.xilihui.xlh.business.activitys.MsgActivity;
import com.xilihui.xlh.business.activitys.PromoCodeActivity;
import com.xilihui.xlh.business.activitys.RobBillActivity;
import com.xilihui.xlh.business.activitys.ScanActivity;
import com.xilihui.xlh.business.activitys.StoreDetailsActivity;
import com.xilihui.xlh.business.entities.AgentsEntity;
import com.xilihui.xlh.business.entities.HomeEntity;
import com.xilihui.xlh.business.fragments.AssistantFragment;
import com.xilihui.xlh.business.requests.SignedRequest;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.view.Banner;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

import rx.functions.Action1;

/**
 * Created by Administrator on 2018/5/17.
 */

public class AssistanAdapter extends RecyclerView.Adapter<BaseViewHolder>{
    
    private static final int HEAD = 0;//顶部
    private static final int SHOP = 1;//门店
    
    private Context context;
    private Banner banner;
    
    String sign_status = "";
    
    ArrayList<HomeEntity.BannerBean> banners = new ArrayList<>();
    ArrayList<AgentsEntity> datas = new ArrayList<>();
    
    Intent intent;
    
    public AssistanAdapter(Context context){
        this.context = context;
    }
    
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType){
            case HEAD:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_head_assistan,parent,false);
                break;
            case SHOP:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_shop,parent,false);                
                break;
        }
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        switch (position){
            case HEAD:
                bindHead(holder);
                break;
            default:
                
                holder.setText(R.id.tv_shopName,datas.get(position).getName());
                holder.setText(R.id.tv_address,datas.get(position).getAddress()+" | "+"约"+
                        datas.get(position).getJuli());
                String str = datas.get(position).getScore();
                if(!TextUtil.isEmpty(str)){
                    float score = Float.parseFloat(str);
                    if(score==0){
                        holder.setVisibility(R.id.ratingBar,View.GONE);
                        holder.setText(R.id.tv_pingfeng,"暂无评分");
                    }else {
                        holder.setVisibility(R.id.ratingBar,View.VISIBLE);
                        holder.setRating(R.id.ratingBar,score);
                        holder.setText(R.id.tv_pingfeng,score+"分");
                    }
                    
                }
               
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv_shopLogo);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+datas.get(position).getBackground_picture_url());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(isLogin()){
                            intent = new Intent(context,StoreDetailsActivity.class);
                            intent.putExtra("id",datas.get(position).getId());
                            context.startActivity(intent);
                        }else {
                            context.startActivity(new Intent(context,LoginActivity.class));
                        }
                        
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        if(datas!=null&&datas.size()>0){
            return datas.size();
        }else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return HEAD;
        }else {
            return SHOP;
        }
    }
    
    
    public void bindHead(final BaseViewHolder holder){

        banner = (Banner) holder.getView(R.id.banner);
        
        banner.setSize(banners.size(), new Banner.Callback() {
            @Override
            public void onItemView(ImageView v, int position) {
                ImageHelper.display(context,v, UrlConst.baseUrl()+banners.get(position).getAd_code());
            }

            @Override
            public void onItemClicked(ImageView v, int position) {
                
            }
        });
        
        banner.fly();
        
         //我要预约
         holder.setOnClickListener(R.id.tv_yaoyuye, new View.OnClickListener() {
             @Override
             public void onClick(View view) {
//                 context.startActivity(new Intent(context,StoreDetailsActivity.class));
                 if(isLogin()){
                     if(sign_status.equals("3")){
                         context.startActivity(new Intent(context,GoAppointmenActivity.class));
                     }else if(sign_status.equals("1")){
                         ToastUtil.toastShortNegative("审核中");
                     }else{
                         context.startActivity(new Intent(context,ApplyActivity.class));
                     }
                 }else {
                     context.startActivity(new Intent(context, LoginActivity.class));
                 }
                
             }
         });
         
         //我的预约
         holder.setOnClickListener(R.id.tv_yuye, new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 if(isLogin()){
                     if(sign_status.equals("3")){
                         context.startActivity(new Intent(context, AppointmentActivity.class));
                     }else if(sign_status.equals("1")){
                         ToastUtil.toastShortNegative("审核中");
                     }else{
                         context.startActivity(new Intent(context,ApplyActivity.class));
                     }
                 }else {
                     context.startActivity(new Intent(context, LoginActivity.class));
                 }
                 
             }
         });
         
         //在线抢单
         holder.setOnClickListener(R.id.tv_qiangdan, new View.OnClickListener() {
             @Override
             public void onClick(View view) {
//                 context.startActivity(new Intent(context, RobBillActivity.class));
                 if(isLogin()){
                     if(sign_status.equals("3")){
                         context.startActivity(new Intent(context, MsgActivity.class));
                     }else if(sign_status.equals("1")){
                         ToastUtil.toastShortNegative("审核中");
                     }else{
                         context.startActivity(new Intent(context,ApplyActivity.class));
                     }
                 }else {
                     context.startActivity(new Intent(context, LoginActivity.class));
                 }
             }
         });
         
         //签约申请
         holder.setOnClickListener(R.id.tv_shengqin, new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 if(isLogin()){
                     SignedRequest.isApply(view.getContext())
                             .compose(DoTransform.<BaseEntity>applyScheduler((FragmentActivity) context,true))
                             .subscribe(new HttpSubscriber<BaseEntity>((Activity) context) {
                                 @Override
                                 protected void onSuccess(BaseEntity entity) {
                                     context.startActivity(new Intent(context, ApplyActivity.class));
                                 }
        
                                 @Override
                                 protected void onFail(String msg) {
                                     ToastUtil.toastShortNegative(msg);
                                 }
                             });
                 }else {
                     context.startActivity(new Intent(context, LoginActivity.class));
                 }
                 
             }
         });
         
         //推广码
         holder.setOnClickListener(R.id.tv_tuiguangma, new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 if(isLogin()){
                     if(sign_status.equals("3")){
                         context.startActivity(new Intent(context, PromoCodeActivity.class));
                     }else if(sign_status.equals("1")){
                         ToastUtil.toastShortNegative("审核中");
                     }else{
                         context.startActivity(new Intent(context,ApplyActivity.class));
                     }
                     
                 }else {
                     context.startActivity(new Intent(context, LoginActivity.class));
                 }
             }
         });
         
         //加入门店
         holder.setOnClickListener(R.id.tv_mendian, new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 if(isLogin()){
                     if(sign_status.equals("3")){
                         context.startActivity(new Intent(context, JoinStoresActivity.class));
                     }else if(sign_status.equals("1")){
                         ToastUtil.toastShortNegative("审核中");
                     }else{
                         context.startActivity(new Intent(context,ApplyActivity.class));
                     }
                     
                 }else {
                     context.startActivity(new Intent(context,LoginActivity.class));
                 }
             }
         });
         
    }
    
    public void setBanner(ArrayList<HomeEntity.BannerBean> bannerData){
        if(banners!=null&&banners.size()>0){
            banners.clear();
        }
        
        banners.addAll(bannerData);
        notifyDataSetChanged();
    }
    
    public void setDatas(ArrayList<AgentsEntity> data){
        if(datas!=null&&datas.size()>0){
            datas.clear();
        }

        AgentsEntity agentItem = new AgentsEntity();
        agentItem.setScore("0");
        datas.add(agentItem);
        datas.addAll(data);
        notifyDataSetChanged();
        
    }
    
    public void setSingStatus(String sign_status){
        this.sign_status = sign_status;
    }

    public boolean isLogin(){
        return (boolean) SPUtil.get(context,SPUtil.IS_LOGIN,false);
    }
    
}
